﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Xml;
namespace Tracy.CoreLib.Data
{
    public class XmlModelCollection : KeyedCollection<string, IXmlModelItem>, IXmlModelCollection
    {
        public XmlModelCollection() : base() { }

        protected override string GetKeyForItem(IXmlModelItem item)
        {
            return item.Key;
        }
    }

    public class ManagedXmlModelCollection : IManagedXmlModelCollection
    {
        IXmlModelCollection container;
        
        private IXmlModelCollection Container
        {
            get { if (container == null) container = new XmlModelCollection(); return container; }
        }

        Dictionary<int, IManagedXmlModelItem> dic;

        public Dictionary<int, IManagedXmlModelItem> Dic
        {
            get { if (dic == null) dic = new Dictionary<int, IManagedXmlModelItem>(); return dic; }
        }
        public ManagedXmlModelCollection()
        {
        }

        public virtual IManagedXmlModelItem GetItemById(int id)
        {
            IManagedXmlModelItem rtn = null;
            Dic.TryGetValue(id, out rtn);
            return rtn;
        }

        public virtual bool ContainsId(int id)
        {
            return dic.ContainsKey(id);
        }

        #region IManagedXmlModelCollection Members

        public IXmlModelItem this[string key]
        {
            get { return Container[key]; }
        }

        public bool Contains(string key)
        {
            return Container.Contains(key);
        }

        /// <summary>
        /// 获取id在集合中的序号
        /// </summary>
        public virtual int IndexOf(int id)
        {
            IManagedXmlModelItem item = null;
            Dic.TryGetValue(id, out item);
            if (item != null)
            {
                return IndexOf(item);
            }
            else
            {
                return -1;
            }
        }

        #endregion

        #region IList<IXmlModelItem> Members

        public int IndexOf(IXmlModelItem item)
        {
            return Container.IndexOf(item);
        }

        public void Insert(int index, IXmlModelItem item)
        {
            if (item is IManagedXmlModelItem)
            {
                IManagedXmlModelItem managedItem = (IManagedXmlModelItem)item;
                Dic.Add(managedItem.Id, managedItem);
            }
            Container.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            IXmlModelItem toDelete = Container[index];
            if(toDelete is IManagedXmlModelItem)Dic.Remove(((IManagedXmlModelItem)toDelete).Id);
            Container.RemoveAt(index);
        }

        public IXmlModelItem this[int index]
        {
            get
            {
                return Container[index];
            }
            set
            {
                Container[index] = value;
            }
        }

        #endregion

        #region ICollection<IXmlModelItem> Members

        public void Add(IXmlModelItem item)
        {
            if (item is IManagedXmlModelItem)
            {
                IManagedXmlModelItem managedItem = (IManagedXmlModelItem)item;
                if (managedItem.Id != -1)
                {
                    Dic.Add(managedItem.Id, managedItem);
                }
            }
            Container.Add(item);
        }

        public void Clear()
        {
            Container.Clear();
            Dic.Clear();
        }

        public bool Contains(IXmlModelItem item)
        {
            return Container.Contains(item);
        }

        public void CopyTo(IXmlModelItem[] array, int arrayIndex)
        {
            foreach (IXmlModelItem item in array)
            {
                if (item is IManagedXmlModelItem)
                {
                    IManagedXmlModelItem managedItem = (IManagedXmlModelItem)item;
                    Dic.Add(managedItem.Id, managedItem);
                }
            }
            Container.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return Container.Count; }
        }

        public bool IsReadOnly
        {
            get { return Container.IsReadOnly; }
        }

        public bool Remove(IXmlModelItem item)
        {
            if (item is IManagedXmlModelItem) Dic.Remove(((IManagedXmlModelItem)item).Id);
            return Container.Remove(item);
        }

        #endregion

        #region IEnumerable<IXmlModelItem> Members

        public IEnumerator<IXmlModelItem> GetEnumerator()
        {
            return Container.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return Container.GetEnumerator();
        }

        #endregion

        public IManagedXmlModelCollection GetCopy()
        {
            IManagedXmlModelCollection copied = new ManagedXmlModelCollection();
            foreach (IManagedXmlModelItem item in this.Container)
                copied.Add(item);
            return copied;
        }
    }
}